<!DOCTYPE html><html lang="en"><head><title>PaymentWithCreditCard.aspx</title></head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0"><meta name="groc-relative-root" content=""><meta name="groc-document-path" content="PaymentWithCreditCard.aspx"><meta name="groc-project-path" content="PaymentWithCreditCard.aspx.cs"><link rel="stylesheet" type="text/css" media="all" href="assets/style.css"><script type="text/javascript" src="assets/behavior.js"></script><body><div id="meta"><div class="file-path">PaymentWithCreditCard.aspx.cs</div></div><div id="document"><div class="segment"><div class="comments "><div class="wrapper"><h1 id="createpayment-using-credit-card-sample">CreatePayment using credit card Sample</h1>
<p>This sample code demonstrate how you can process
a payment with a credit card.
API used: /v1/payments/payment</p></div></div><div class="code"><div class="wrapper"><span class="hljs-keyword">using</span> PayPal.Api;
<span class="hljs-keyword">using</span> System.Collections.Generic;

namespace PayPal.Sample
{
    <span class="hljs-keyword">public</span> <span class="hljs-keyword">partial</span> <span class="hljs-keyword">class</span> PaymentWithCreditCard : BaseSamplePage
    {
        <span class="hljs-keyword">protected</span> <span class="hljs-keyword">override</span> <span class="hljs-keyword">void</span> <span class="hljs-title">RunSample</span>()
        {</div></div></div><div class="segment"><div class="comments "><div class="wrapper"><h3 id="api-context">Api Context</h3>
<p>Pass in a <code>APIContext</code> object to authenticate 
the call and to send a unique request id 
(that ensures idempotency). The SDK generates
a request id if you do not pass one explicitly. 
See <a href="/Source/Configuration.html">Configuration.cs</a> to know more about APIContext.</p></div></div><div class="code"><div class="wrapper">            <span class="hljs-keyword">var</span> apiContext = Configuration.GetAPIContext();</div></div></div><div class="segment"><div class="comments "><div class="wrapper"><p>A transaction defines the contract of a payment - what is the payment for and who is fulfilling it. </p></div></div><div class="code"><div class="wrapper">            <span class="hljs-keyword">var</span> transaction = <span class="hljs-keyword">new</span> Transaction()
            {
                amount = <span class="hljs-keyword">new</span> Amount()
                {
                    currency = <span class="hljs-string">"USD"</span>,
                    total = <span class="hljs-string">"7"</span>,
                    details = <span class="hljs-keyword">new</span> Details()
                    {
                        shipping = <span class="hljs-string">"1"</span>,
                        subtotal = <span class="hljs-string">"5"</span>,
                        tax = <span class="hljs-string">"1"</span>
                    }
                },
                description = <span class="hljs-string">"This is the payment transaction description."</span>,
                item_list = <span class="hljs-keyword">new</span> ItemList()
                {
                    items = <span class="hljs-keyword">new</span> List&lt;Item&gt;()
                    {
                        <span class="hljs-keyword">new</span> Item()
                        {
                            name = <span class="hljs-string">"Item Name"</span>,
                            currency = <span class="hljs-string">"USD"</span>,
                            price = <span class="hljs-string">"1"</span>,
                            quantity = <span class="hljs-string">"5"</span>,
                            sku = <span class="hljs-string">"sku"</span>
                        }
                    },
                    shipping_address = <span class="hljs-keyword">new</span> ShippingAddress
                    {
                        city = <span class="hljs-string">"Johnstown"</span>,
                        country_code = <span class="hljs-string">"US"</span>,
                        line1 = <span class="hljs-string">"52 N Main ST"</span>,
                        postal_code = <span class="hljs-string">"43210"</span>,
                        state = <span class="hljs-string">"OH"</span>,
                        recipient_name = <span class="hljs-string">"Joe Buyer"</span>
                    }
                },
                invoice_number = Common.GetRandomInvoiceNumber()
            };</div></div></div><div class="segment"><div class="comments "><div class="wrapper"><p>A resource representing a Payer that funds a payment.</p></div></div><div class="code"><div class="wrapper">            <span class="hljs-keyword">var</span> payer = <span class="hljs-keyword">new</span> Payer()
            {
                payment_method = <span class="hljs-string">"credit_card"</span>,
                funding_instruments = <span class="hljs-keyword">new</span> List&lt;FundingInstrument&gt;()
                {
                    <span class="hljs-keyword">new</span> FundingInstrument()
                    {
                        credit_card = <span class="hljs-keyword">new</span> CreditCard()
                        {
                            billing_address = <span class="hljs-keyword">new</span> Address()
                            {
                                city = <span class="hljs-string">"Johnstown"</span>,
                                country_code = <span class="hljs-string">"US"</span>,
                                line1 = <span class="hljs-string">"52 N Main ST"</span>,
                                postal_code = <span class="hljs-string">"43210"</span>,
                                state = <span class="hljs-string">"OH"</span>
                            },
                            cvv2 = <span class="hljs-string">"874"</span>,
                            expire_month = <span class="hljs-number">11</span>,
                            expire_year = <span class="hljs-number">2018</span>,
                            first_name = <span class="hljs-string">"Joe"</span>,
                            last_name = <span class="hljs-string">"Shopper"</span>,
                            number = <span class="hljs-string">"4877274905927862"</span>,
                            type = <span class="hljs-string">"visa"</span>
                        }
                    }
                },
                payer_info = <span class="hljs-keyword">new</span> PayerInfo
                {
                    email = <span class="hljs-string">"test@email.com"</span>
                }
            };</div></div></div><div class="segment"><div class="comments "><div class="wrapper"><p>A Payment resource; create one using the above types and intent as <code>sale</code> or <code>authorize</code></p></div></div><div class="code"><div class="wrapper">            <span class="hljs-keyword">var</span> payment = <span class="hljs-keyword">new</span> Payment()
            {
                intent = <span class="hljs-string">"sale"</span>,
                payer = payer,
                transactions = <span class="hljs-keyword">new</span> List&lt;Transaction&gt;() { transaction }
            };</div></div></div><div class="segment"><div class="code folded"><div class="wrapper marker"><span class="c1">            // Ignore workflow code segment</span></div><div class="wrapper">            <span class="hljs-comment">// Ignore workflow code segment</span>
            <span class="hljs-preprocessor">#<span class="hljs-keyword">region</span> Track Workflow</span>
            <span class="hljs-keyword">this</span>.flow.AddNewRequest(<span class="hljs-string">"Create credit card payment"</span>, payment);
            <span class="hljs-preprocessor">#<span class="hljs-keyword">endregion</span></span></div></div></div><div class="segment"><div class="comments "><div class="wrapper"><p>Create a payment using a valid APIContext</p></div></div><div class="code"><div class="wrapper">            <span class="hljs-keyword">var</span> createdPayment = payment.Create(apiContext);</div></div></div><div class="segment"><div class="code folded"><div class="wrapper marker"><span class="c1">            // Ignore workflow code segment</span></div><div class="wrapper">            <span class="hljs-comment">// Ignore workflow code segment</span>
            <span class="hljs-preprocessor">#<span class="hljs-keyword">region</span> Track Workflow</span>
            <span class="hljs-keyword">this</span>.flow.RecordResponse(createdPayment);
            <span class="hljs-preprocessor">#<span class="hljs-keyword">endregion</span></span></div></div></div><div class="segment"><div class="comments "><div class="wrapper"><p>For more information, please visit <a href="https://developer.paypal.com/docs/api/">PayPal Developer REST API Reference</a>.</p></div></div><div class="code"><div class="wrapper">        }
    }
}</div></div></div></div></body></html>